package com.aaa.controller;

import com.aaa.entity.Melody;
import com.aaa.entity.Mtype;
import com.aaa.entity.Singer;
import com.aaa.service.MelodyService;
import com.aaa.service.MtypeService;
import com.aaa.service.SingerService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.MelodypageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/Melody")
public class MelodyController {

    @Autowired
    private MelodyService melodyService;

    @Autowired
    private MtypeService mtypeService;

    @Autowired
    private SingerService singerService;

    //保存图片的文件地址
    public static final String IMG_PATH="D:/upload/";

    @RequestMapping("/listMyMelody")
    public MelodypageInfo<Melody> listAll(Integer currentPage, Integer pageSize, String pramykey){
        MelodypageInfo<Melody> page = melodyService.page(currentPage, pageSize, pramykey);
        return page;
    }


    @RequestMapping("/upload")
    @ResponseBody
    public DefaultMsg upload(MultipartFile file){
        DefaultMsg defaultMsg = new DefaultMsg();
        System.out.println("图片上传被调用");
        //使用UUID为文件生成唯一的文件名
        String string = UUID.randomUUID().toString();
        File targetFile = new File(IMG_PATH+string);
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
            defaultMsg.setSuccess(0);
            defaultMsg.setMsg("文件上传失败");
        }
        defaultMsg.setTarget(string);
        return defaultMsg;
    }

    @RequestMapping("/Lrcupload")
    @ResponseBody
    public DefaultMsg Lrcupload(MultipartFile file){
        DefaultMsg defaultMsg = new DefaultMsg();
        String name = file.getOriginalFilename();
        defaultMsg.setTarget(name);
        return defaultMsg;
    }


    @RequestMapping("/selectMtype")
    public List<Mtype> selectMtype(){
        List<Mtype> mtypes = mtypeService.listAll();
        return mtypes;
    }

    @RequestMapping("/selectSinger")
    public List<Singer> selectSinger(){
        List<Singer> singers = singerService.listMySinger();
        return singers;
    }

    @RequestMapping("/onSave")
    public DefaultMsg onSave(Melody melody,String singerid,String mtid){
        Mtype mtype = new Mtype();
        mtype.setMtid(Long.valueOf(mtid));
        Singer singer = new Singer();
        singer.setSid(Integer.valueOf(singerid));
        melody.setMtype(mtype);
        melody.setSinger(singer);
        DefaultMsg defaultMsg = melodyService.onSave(melody);
        return new DefaultMsg();
    }

    @RequestMapping("/MelodyDelete")
    public DefaultMsg MelodyDelete(Integer mid){
        DefaultMsg defaultMsg = melodyService.MelodyDelete(mid);
        return defaultMsg;
    }
}
